<template>
	<!-- 设置 -->
	<view class="container">
		<view class="list-cell b-b m-t">
			<text class="cell-tit" @click="go_getuser">个人资料</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<view class="list-cell b-b" @click="go_change_password">
			<text class="cell-tit">修改密码</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<view class="list-cell m-t b-b" >
			<text class="cell-tit">清除缓存</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<view class="list-cell b-b" @click="navTo('/pages/set/about')">
			<text class="cell-tit">关于小莱</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
		<!-- #ifdef APP-PLUS -->
	
		<view class="list-cell" >
			<text class="cell-tit">检查更新</text>
			<text class="cell-tip">当前版本 1.0</text>
			<text class="cell-more yticon icon-you"></text>
		</view>
			<!-- #endif -->
		<view class="list-cell log-out-btn" @click="set_clear" v-if="user_log"><text class="cell-tit">退出登录</text></view>

		
	</view>
</template>

<script>
export default {
	data(){
		return{
			timer: null,
			user_log:true//用户是否已登陆
		}
	},
	onUnload() {
		//注销定时器
		if(this.timer) { 
			console.log('注销定时器')
			clearTimeout(this.timer);  
			this.timer = null;
		} 
	},
	// onShow页面显示就执行
	onShow() {
		// 获取本地数据API
		uni.getStorage({
			key: 'token',
			success: res => {
				// 数据获取成功 用户已登陆
				console.log(res);
			},
			fail: err => {
				// uni.$u.toast("尚未登陆，请先登录")
				console.log('您还未登录');
				this.user_log=false
			}
		});
	},
	methods:{
		// 获取用户信息
		go_getuser(){
			let token=uni.getStorageSync('token')
			this.token=token;
			uni.request({
				method:'POST',
				data:{
					'token':this.token
				},
				url:this.host+'/user/getuserInfo',
				success:(res) => {
					console.log(res);
					this.userlist=res.data.userInfo
					if(this.userlist==undefined){
						uni.$u.toast("请先登录")
						uni.clearStorageSync()
						this.timer = setTimeout( () => {
							uni.navigateTo({
								url: '/pages/public/login'
							})
						}, 500)
					}
				},
				fail: () => {
					console.log('请求失败');
				} 
			})
		},
		// 修改密码
		go_change_password(){
			uni.getStorage({
				key: 'token',
				success: res => {
					// 数据获取成功 用户已登陆
					console.log(res);
					uni.navigateTo({
						url:'/pages/public/edit_pwd'
					})
				},
				fail: err => {
					uni.$u.toast("请先登录")
					this.timer = setTimeout( () => {
						uni.navigateTo({
							url: '/pages/public/login'
						})
					}, 500)
				}
			});
		},
		// 退出登录
		set_clear(){
			uni.showModal({
				title: '提示',
				confirmText: '是',
				cancelText: '否',
				content: '是否要退出登录',
				success: function(res) {
					if (res.confirm) {
						console.log('用户点击确定');
						uni.clearStorageSync(); //清除所有token
						uni.$u.toast('退出成功')
						
						//使用定时器
						this.timer = setTimeout( () => {
							uni.switchTab({
								url:'/pages/index/index'
							})
						}, 500)

					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		}
	}
};
</script>

<style lang="scss">
page {
	background: $page-color-base;
}
.list-cell {
	display: flex;
	align-items: baseline;
	padding: 20upx $page-row-spacing;
	line-height: 60upx;
	position: relative;
	background: #fff;
	justify-content: center;
	&.log-out-btn {
		margin-top: 40upx;
		.cell-tit {
			color: $uni-color-primary;
			text-align: center;
			margin-right: 0;
		}
	}
	&.cell-hover {
		background: #fafafa;
	}
	&.b-b:after {
		left: 30upx;
	}
	&.m-t {
		margin-top: 16upx;
	}
	.cell-more {
		align-self: baseline;
		font-size: $font-lg;
		color: $font-color-light;
		margin-left: 10upx;
	}
	.cell-tit {
		flex: 1;
		font-size: $font-base + 2upx;
		color: $font-color-dark;
		margin-right: 10upx;
	}
	.cell-tip {
		font-size: $font-base;
		color: $font-color-light;
	}
	switch {
		transform: translateX(16upx) scale(0.84);
	}
}
</style>
